package com.projectsierra.springappengine;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.logging.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Query;
import com.walczak.piotr.Decrypt;
public class LoginServlet extends HttpServlet {
	private static final Logger log = Logger.getLogger(RegisterServlet.class.getName());

	 public void doPost(HttpServletRequest req, HttpServletResponse resp)
             throws IOException {
		 String a= req.getParameter("email");
		 String b= req.getParameter("password");
		 DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		 Key guestbookKey = KeyFactory.createKey("Uzytkownik", "Uzytkownik");
		 Query query = new Query("Uzytkownik", guestbookKey);
		 
		 List<Entity> greetings = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(100));
		 log.info("DUUUPA");
		 for (Entity greeting : greetings) {
	        
			 try {
				if(greeting.getProperty("email").equals(a) && greeting.getProperty("password").equals(Decrypt.getDecrypt(b)))
				 
				 {
					 resp.sendRedirect("index.html");
					 log.info("JEST");
				 } 
				 else
				 {
					 resp.sendRedirect("Test.jsp"); 
					 log.info("NIE MA");
				 }
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				log.info("BLAD");
			}
		 }

		  

   
 }
	 
	  public void doGet(HttpServletRequest request, HttpServletResponse response)
	    		throws IOException{
		  
	
	  }

}
